//
//  PictureViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/10/15.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "PictureViewController.h"
#import "GetDataMethods.h"
#import "Items.h"
#import "ItemCollectionViewCell.h"
#import "MJRefresh.h"
#import "Model.h"
#import "MBProgressHUD.h"
#import "Fit.h"

@interface PictureViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *dataArray;
@property(nonatomic, assign)NSInteger num;
@property(nonatomic, retain)MBProgressHUD *progressView;

@end

@implementation PictureViewController
-(void)dealloc
{
    [_collectionView release];
    [_dataArray release];
    [_progressView release];
    [super dealloc];
}

-(void)changeColor{
    self.collectionView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"图库";
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataArray = [NSMutableArray array];
    [self reloadDataAndRefresh];
    [self changeColor];
    [self getData];
}
-(void)getData
{
    [GetDataMethods getDataByString:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/photos/?start=%ld&count=21&gallery_mode=true", _number, self.string, _num] BodyString:nil WithDataBlock:^(id data) {
        NSArray *array = [data objectForKey:@"items"];
        for (NSDictionary *dic in array) {
            Items *item = [[Items alloc]init];
            [item setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:item];
        }
        [_collectionView reloadData];
        [_collectionView.header endRefreshing];//停止刷新
        [_collectionView.footer endRefreshing];//停止加载
        [_progressView removeFromSuperview];//移除加载中视图
        
    }];
}
-(void)reloadDataAndRefresh
{
    _collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataArray removeAllObjects];
        _num = 0;
        [self getData];
    }];
    _collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _num += 21;
        [self getData];
    }];
}

-(void)loadView
{
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(120 * W, 120 * H);
    flowLayout.minimumLineSpacing = 7.5 * W;
    flowLayout.minimumInteritemSpacing = 7.5 * H;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    [_collectionView registerClass:[ItemCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    
    //加载视图初始化
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"玩命加载中";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.77 alpha:.3];
    [self.view addSubview:_progressView];
    [_progressView release];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    if (_dataArray.count != 0) {
        cell.item = _dataArray[indexPath.row];
    }
    cell.backgroundColor = [Model shareInstance].BackgroundColor;
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
